import cv2

cap = cv2.VideoCapture(0)  # 打开默认摄像头

# 设置视频编码器和比特率
fourcc = cv2.VideoWriter_fourcc(*'XVID')  # 使用XVID编码器
frame_width = 1920  # 设置宽度为1920像素
frame_height = 1080  # 设置高度为1080像素
bit_rate = frame_width * frame_height * 20  # 设置比特率为1920x1080x20=40960000
out = cv2.VideoWriter('output.avi', fourcc, 20.0,
                      (frame_width, frame_height), True)  # 创建VideoWriter对象

while (cap.isOpened()):
    ret, frame = cap.read()
    if ret:
        out.write(frame)  # 将帧写入视频

        cv2.imshow('frame', frame)  # 显示帧
        if cv2.waitKey(1) & 0xFF == ord('q'):  # 按'q'键退出
            break
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()
